package controllers;

import play.*;
import play.mvc.*;

import java.util.*;

import models.*;

@With(Secure.class)
// this class is used for the login, Doaa El-Hawary
public class Relationships extends Controller {

	public static void index() {
		render();
	}

	/**
	 * A method that creates relationships among entities
	 * 
	 * @author Ebtehal Abed
	 * @version 2011.04.26
	 * @category C2S2
	 * @params String name: relation name, Long entitySourceID: source entity to
	 *         be related, Long entityDestinationID: destination entity the will
	 *         be related to the source, Long organizationID: id of the
	 *         organization inwhich the relation will be created
	 * 
	 * @return void
	 */
	public static void createEntityRelations(String name, Long entitySourceID,
			Long entityDestinationID, Long organizationID) {

		String username = Security.connected();
		Eentity source = Eentity.findById(entitySourceID);
		Eentity destination = Eentity.findById(entityDestinationID);
		Organization organization = Organization.findById(organizationID);
		User user = User.findByUsername(username);
		String result = "";

		if (user.getUserType(organization).equalsIgnoreCase("Lead")) {
			if (source.getName() != destination.getName()) {
				for (int i = 0; i < organization.getRelations().size(); i++) {
					if (organization.getRelations().get(i).getName()
							.equalsIgnoreCase(name)) {
						result = "Relation already exists";
						renderTemplate("Relationships/viewRelation", result);
					}
				}
				for (int j = 0; j < organization.getRelatedEntities(source).size(); j++) {

					if (organization.getRelatedEntities(source).equals(destination)) {

						result = "CAUTION: Entities are already related";
						renderTemplate("Relationships/viewRelation", result);
					}

				}
				Relationship relation = new Relationship(name, source,
						destination, true);
				relation.save();
				organization.getRelations().add(relation);
				source.getRelatedEntities().add(destination);

				result = source.getName() + " " + name + " "
						+ destination.getName();
				renderTemplate("Relationships/viewRelation", result);

			} else {

				result = "Conflict: Cannot relate same entity";
				renderTemplate("Relationships/viewRelation", result);
			}
		} else {
			result = "You are not authorized to create relations";
			renderTemplate("Relationships/viewRelation", result);
		}
	}

	public static void viewRelations(Long id) {

		Relationship r = Relationship.findById(id);
		render(r);
	}

	/**
	 * Creating Relationship Among tags
	 * 
	 * @author Ibrahim Kessba
	 * @version 2011.05.02
	 * @category C2S13
	 * @params String name, Long tagSourceID, Long tagDestinationID, Long
	 *         organizationID: Orgnaiztion Holding this Tags Creation Long
	 *         userID: User Id To Create this Relationship <Organizer Rank>
	 * @return boolean -> True : Relationship Created Correctly , False :
	 *         Failing Scenario
	 */

	public static boolean createTagsRelations(String name, Long tagSourceID,
			Long tagDestinationID, Long organizationID) {

		Tag source = Tag.findById(tagSourceID);
		Tag destination = Tag.findById(tagDestinationID);

		Organization o = Organization.findById(organizationID);

		// Getting the User Object Using the Id

		String username = Security.connected();

		User user = User.findByUsername(username);

		if (user.getUserType(o).equalsIgnoreCase("Organizer")) {

			if (source.getName() != destination.getName()) {
				for (int i = 0; i < o.getRelations().size(); i++) {
					if (o.getRelations().get(i).getName()
							.equalsIgnoreCase(name))
						return false;
				}
				/*
				 * for (int j = 0; j < o.getTags(source) .size(); j++) {
				 * 
				 * if (o.getRelatedEntities(source).equals( destination)) return
				 * false; }
				 */

				Relationship relation = new Relationship(name, source,
						destination, true);
				// Neeed to be added in the relationship model constractor
				relation.save();
				o.getRelations().add(relation);

				render(name, source, destination);

			}
			return true;

		}
		return true;
	}

	public static void newRelationshipTagsForm(long OrganizationId) {

		Organization organizationTemp = Organization.findById(OrganizationId);

		 LinkedList<Tag> tagsList = organizationTemp.getAllTags();

		/*
		 * LinkedList tagsList = new LinkedList(); tagsList.add("a");
		 * tagsList.add("b"); tagsList.add("c");
		 */
		 
		render(tagsList);
	}

	public static void relationshipHome(String msg) {

		if (msg == null) {
			msg = "Default Message";
		}

		render(msg);
	}

	public static void newRelationshipTags() {
		String msg = "Sucess :testing Trial";
		relationshipHome(msg);
		render();
	}

	public static void relationshipShow(long relationshipId) {

		Relationship temp = Relationship.findById(relationshipId);

		String name = temp.getName();

		render(name);
	}

	public static void viewAllTagsRelationship() {

		List allTagsTempList = models.Relationship.findAll();
		render(allTagsTempList);
	}
}
